<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/5/11
 * Time: 11:00
 */
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Request;
class Management extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';

    //候车点名
    public function riding()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }
        $info = $this->auth->getUserinfo();

        $params = input('get.');
        $where = [];
        if(!isset($params['circuit_id']) || $params['circuit_id']=='')
        {
            $this->error('缺少参数');
        }

        $where['a.circuit_id'] = $params['circuit_id'];

        $res = Db::name('sign_in')->where(['time'=>date('Y-m-d'),'circuit_id'=>$params['circuit_id'],'admin_id'=>$this->auth->id])->find();

        $status = 0;
        if($res)
        {
            $status = 1;
        }
        $class_room = Db::name('class_room')->field('id,title')->select();
        $class_arr = [];
        if($class_room)
        {
            foreach ($class_room as $k=>$v)
            {
                $class_arr[$v['id']] = $v['title'];
            }
        }


        $student = Db::name('automatic_student')->alias('a')
            ->join('fa_on_off_waiting b','a.RC_NUM=b.RC_NUM','left')
            ->where(['a.circuit_id'=>['in',$info['circuit_id']]])
            ->where($where)
            ->field('a.id,a.RC_NUM,a.class_room_id,a.point,a.name,a.circuit_id,a.state,b.circuit,b.RC_NUM as status')

            ->select();

        if($student)
        {
            foreach ($student as $key=>&$value)
            {
                $value['class_room'] = isset($class_arr[$value['class_room_id']])?$class_arr[$value['class_room_id']]:'';
                if($value['status']==NULL)
                {
                    $value['status'] = '未打卡';
                }
                else
                {
                    $value['status'] = '已打卡';
                }
            }
        }
        $this->success('',['data'=>$student,'status'=>$status]);

    }

    public function get_status()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }
        $arr = [
            0=>'正常',
            1=>'请假',
            2=>'临时请假',
            3=>'学习',
            4=>'留校',
            5=>'丢卡'
        ];

        $this->success('',$arr);
    }


    //学生状态
    public function student_status()
    {
        if (!Request::instance()->isPost())
        {
            $this->error('请求方式错误');
        }

        $params = input('post.');
        if(!isset($params['id']) || $params['id']=='')
        {
            $this->error('却少参数');
        }

        if(!isset($params['state']) || $params['state']=='')
        {
            $this->error('却少参数');
        }

        if($params['state']==5)
        {
            $student = Db::name('automatic_student')->alias('a')
                        ->join('fa_student_information b','a.student_id=b.id')
                        ->where('a.id',$params['id'])
                        ->field('b.name')
                        ->find();

            $time = time();
            $data = [];
            $data['state'] = 4;
            $data['content'] = $student.'同学丢失了校园乘车卡，请及时督促补卡';
            $data['time'] = date('Y-m-d');
            $data['createtime'] = $time;
            $data['updatetime'] = $time;
            Db::name('abnormal_information')->insert($data);
        }

        $res = Db::name('automatic_student')->where('id',$params['id'])->update(['state'=>$params['state']]);
        if($res)
        {
            $this->success('更新成功');
        }
        else
        {
            $this->error('更新失败');
        }
    }

    public function abnormal_information()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }

        $params = input('get.');
        //1=上下车异常,2=车辆异常,3=线路异常
        $arr = [
          1=>'上下车异常',
          2=>'车辆异常',
          3=>'线路异常',
          4=>'线路异常',
        ];

        $list = Db::name('abnormal_information')->paginate($params['listRows'],false,['page'=>$params['page']])->toArray();
        if($list['data'])
        {
            foreach ($list['data'] as $k=>$v)
            {
                $list['data'][$k]['state'] = $arr[$v['state']];
                $list['data'][$k]['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
            }
            $this->success('',$list);
        }
        else
        {
            $this->error('暂无数据');
        }
    }

    public function sign_in()
    {
        if (!Request::instance()->isPost())
        {
            $this->error('请求方式错误');
        }

        $params = input('post.');
        if(!isset($params['circuit_id']) || $params['circuit_id']=='')
        {
            $this->error('缺少参数');
        }
        $time = time();
        $data = [];
        $data['circuit_id'] = $params['circuit_id'];
        $data['state'] = 1;
        $data['admin_id'] = $this->auth->id;
        $data['time'] = date('Y-m-d');
        $data['createtime'] = $time;
        $data['updatetime'] = $time;
        Db::startTrans();
        try{
            Db::name('sign_in')->insert($data);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->error('点名失败'.$e->getMessage());
        }
        $this->success('点名完毕');


    }


}